# women and SFMV table  ---------------------------------------------------

sfmv_ug1 <- lm(sfmv ~ woman + community, subset(full_data,country == "ug" & wave == 1))
sfmv_ug2 <- lm(sfmv ~ woman + community, subset(full_data,country == "ug" & wave == 2))
sfmv_ug3 <- lm(sfmv ~ woman + community, subset(full_data,country == "ug" & wave == 3))
# Information on gender is missing for two respondents in the first Tanzania wave
# Hence, these respondents are dropped from this analysis.
sfmv_ta <- lm(sfmv ~ woman + community, subset(full_data,country == "ta" & wave == 1))

# Tanzania 2 broken down by gender of victim
sfmv_ta2_victim_f <- lm(sfmv ~ woman + community, subset(tan2, female_victim == 1))
sfmv_ta2_victim_m <- lm(sfmv ~ woman + community, subset(tan2, female_victim == 0))

# Tanzania 3 broken down by gender of victim
sfmv_ta3_victim_f <- lm(sfmv ~ woman + community, subset(tan3, female_victim == 1))
sfmv_ta3_victim_m <- lm(sfmv ~ woman + community, subset(tan3, female_victim == 0))


sfmv_sa <- lm(sfmv ~ woman + community, subset(full_data,country == "sa" & wave == 1))
sfmv_afro <- lm(sfmv ~ woman + region, afro)
sfmv_pooled <- lm(sfmv ~ woman + community, subset(full_data, not_panel == 1))


afro_sfmv_m <- 
  afro %>% group_by(country) %>% filter(woman == 0) %>% 
  summarize(sfmv_m = mean(sfmv,na.rm = TRUE)) %>% 
  ungroup() %>% with(., mean(sfmv_m))


# Get HC1 robust standard errors from estimatr package
ses<- starprep(sfmv_ug1,
        sfmv_ug2,
        sfmv_ug3,
        sfmv_ta,
        sfmv_ta2_victim_f,
        sfmv_ta2_victim_m,
        sfmv_ta3_victim_f,
        sfmv_ta3_victim_m,
        sfmv_sa,
        sfmv_pooled,
        sfmv_afro, stat = "std.error",se_type = "HC1")

pvals<- starprep(sfmv_ug1,
               sfmv_ug2,
               sfmv_ug3,
               sfmv_ta,
               sfmv_ta2_victim_f,
               sfmv_ta2_victim_m,
               sfmv_ta3_victim_f,
               sfmv_ta3_victim_m,
               sfmv_sa,
               sfmv_pooled,
               sfmv_afro, stat = "p.value",se_type = "HC1")


sink("04_manuscript/tables/women_sfmv.tex")
stargazer(
  sfmv_ug1,
  sfmv_ug2,
  sfmv_ug3,
  sfmv_ta,
  sfmv_ta2_victim_f,
  sfmv_ta2_victim_m,
  sfmv_ta3_victim_f,
  sfmv_ta3_victim_m,
  sfmv_sa,
  sfmv_pooled,
  sfmv_afro,
  se = ses,
  p = pvals,
  # type = "text",
  omit.stat = c("rsq","f","ser"), 
  keep = "woman",
  column.separate = c(1, 1, 1, 
                      1, 2, 2, 
                      1, 1, 1),
  # column.labels = c("UG 1","Tanzania","South Africa","Pooled","Afrobarometer"),
  column.labels = c("Ug. 1", "Ug. 2", "Ug. 3",
                    "Tan. 1",
                    "Tan. 2",
                    "Tan. 3",
                    "S.A.","Pooled","Afrobar."),
  covariate.labels = "Woman",
  add.lines = list(
    c(
      "Avg. men",
      round(c(with(subset(full_data,country == "ug" & wave == 1),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(full_data,country == "ug" & wave == 2),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(full_data,country == "ug" & wave == 3),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(full_data,country == "ta" & wave == 1),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(tan2,female_victim == 1),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(tan2,female_victim == 0),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(tan3,female_victim == 1),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(tan3,female_victim == 0),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(full_data,country == "sa" & wave == 1),mean(sfmv[woman == 0],na.rm = TRUE)),
              with(subset(full_data,not_panel == 1),mean(sfmv[woman == 0],na.rm = TRUE)),
              afro_sfmv_m),2)),
    c("Area FE",rep("Yes",11)),
    c("Mob target",c("Driver","Driver","Thief","Driver","Thief","Thief","Thief","Thief","Driver","Mix","")),
    c("Crime victim", c("W","W","W",
                             "W",
                             "W","M",
                             "W","M",
                             "W","Mix",""))
  ),
  no.space = TRUE,dep.var.caption = "",float = F,
  
  dep.var.labels = "Mob Vigilantism Preferred over Police Intervention"
)
sink()

rm(sfmv_ug1,
   sfmv_ug2,
   sfmv_ug3,
   sfmv_ta,
   sfmv_ta2_victim_f,
   sfmv_ta2_victim_m,
   sfmv_ta3_victim_f,
   sfmv_ta3_victim_m,
   sfmv_sa,
   sfmv_pooled,
   sfmv_afro,ses,pvals)